package com.hh.vietnameseproject.ui.me

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import com.hh.vietnameseproject.ui.me.record.RecordActivity
import com.hh.vietnameseproject.ui.person.PersonActivity
import kotlinx.android.synthetic.main.me_fragment.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class MeFragment:Fragment() {
    lateinit var meName :TextView
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.me_fragment,container,false)
        meName = view.findViewById<TextView>(R.id.meName)
        val meId = view.findViewById<TextView>(R.id.meId)
        val meTextExp = view.findViewById<TextView>(R.id.meTextExp)
        val meDay = view.findViewById<TextView>(R.id.meTextDay)

        val infoImage = view.findViewById<LinearLayout>(R.id.meInfoLayout)
        val playCard = view.findViewById<LinearLayout>(R.id.meRecordLayout)
        val record = view.findViewById<LinearLayout>(R.id.mePlayRecordLayout)
        val main = activity as MainActivity

        meName.text = main.data.username
        meId.text = main.data.id.toString()
        meTextExp.text = main.data.experience.toString()
        meDay.text = main.data.datecount.toString()

        GlobalScope.launch {
            var body = AllNetWork.getHeadImage(main.data.id.toString())
            withContext(Dispatchers.Main) {
                meImageView.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
        infoImage.setOnClickListener {
            PersonActivity.actionStart(context,main.data.id.toString(),main.data.username)
        }
        playCard.setOnClickListener {
            GlobalScope.launch {
                var body = AllNetWork.clickCard()
                val jsonObject = JSONObject(body.string())
                withContext(Dispatchers.Main) {
                    if (jsonObject.getInt("code")==1){
                        meTextExp.text = (meTextExp.text.toString().toInt()+50).toString()
                        meDay.text = (meDay.text.toString().toInt()+1).toString()
                    }
                    Toast.makeText(context,"${jsonObject.getString("message")}",Toast.LENGTH_SHORT).show()
                }
            }

        }
        record.setOnClickListener {
            startActivity(Intent(context,RecordActivity::class.java))
        }

        return view
    }
    fun changeName(name:String){
        meName.text = name
    }
}